/**
 * @fileOverview drag add edge from anchor
 * @author huangtonger@aliyun.com
 */
const Page = require('../../base');
const Util = require('../util/');

Page.registerBehaviour('dragAnchorAddEdge', diagram => {
  const graph = diagram.getGraph();
  const rootGroup = graph.getRootGroup();
  graph.behaviourOn('anchor:dragstart', ev => {
    if (ev.button === 2) {
      return;
    }
    const anchor = ev.shape;
    const centerPoint = anchor.get('freezePoint');
    const sourceItem = anchor.getItem();
    const addModel = Util.clone(diagram.get('addModel'));
    const sourceAnchor = anchor.getIndex();
    addModel.source = sourceItem.id;
    addModel.sourceAnchor = sourceAnchor;
    const startPoint = {
      x: centerPoint.x,
      y: centerPoint.y
    };
    const delegation = diagram.getDelegation([{ isEdge: true }], rootGroup);
    diagram.setSignal('dragEdge', true);
    diagram.dragEdgeBeforeShowAnchor(sourceItem, sourceAnchor, 'target');
    diagram.set('addEdgeConfig', {
      addModel,
      delegation,
      startPoint,
      sourceItem
    });
  });
}, [ 'processAddEdge', 'dragHoverAnchorHotspot', 'hoverAnchorActived' ]);
